package com.javabase.study.IOTecStudy;

import java.io.ByteArrayInputStream;
import java.io.IOException;

/**
 * TODO
 * 内容：字节数组流学习
 * 作者:jxx
 * 创建时间：2021/5/14
 */
public class TestByteArrayInputStream {
    public static void main(String[] args){
        byte[] b = "abcdefg".getBytes();
        test(b);
    }
    public static void test(byte[] b){
        ByteArrayInputStream bais = null;
        StringBuilder sb = new StringBuilder();
        int tmp = 0;
        int num = 0;

        try {
            bais = new ByteArrayInputStream(b);
            while ((tmp = bais.read()) != -1) {
                sb.append((char) tmp);
                num++;
                System.out.println((char) tmp);
            }
            System.out.println(sb);
            System.out.println("读取字节数：" + num);
        }finally {
            try {
                if(bais != null){
                    bais.close();
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }
}
